package message.types.stalkerstring;

import java.io.IOException;

import message.Message;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import stalker.StalkerParser;

public abstract class StalkerStringMessage extends Message {
  
  private final Stalker credential_;
  private final String str_;
  
  /**
   * Creates a new login success acknowledgement for the corresponding id
   */
  public StalkerStringMessage(Stalker client, Stalker destination, Stalker credential, String str) {
    super(client, destination);
    assert credential != null;
    assert str != null;
    credential_ = credential;
    str_ = str;
  }
  
  /**
   * Retrieves this message over the net
   * @throws IOException 
   */
  public StalkerStringMessage(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert reader != null;
    credential_ = StalkerParser.retrieve(reader);
    str_ = reader.readString();
    assert (str_ != null && credential_ != null);
  }
  
  public Stalker getCredential() {
    return credential_;
  }
  
  public String getString() {
    return str_;
  }

  public boolean contentEquals(Object obj) {
    if (!(obj instanceof StalkerStringMessage)) {
      return false;
    }
    StalkerStringMessage that = (StalkerStringMessage) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getCredential().equals(that.getCredential()) &&
           this.getString().equals(that.getString());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    StalkerParser.send(credential_, writer);
    writer.writeString(str_);
  }
  
}
